<?php

namespace app\common\services;

use app\components\oss\qiniu\QiniuOss;
use yii\web\UploadedFile;

class UploadService extends BaseService{

    /**
     * 图片上传
     * @param UploadedFile $file
     * @return bool
     */
    public static function uploadFile($file){
        if(!$file){
            self::setError(\Yii::t("app","没有文件上传"));
            return false;
        }

        $file_name = uniqid().substr($file->name,strpos($file->name,"."));

        /**
         * @var QiniuOss $oss
         */
        $oss = \Yii::$app->get("oss");
        if($oss->uploadFile($file_name,$file->tempName)){
            return $oss->bucket_domain.$file_name;
        }else{
            return false;
        }
    }

    /**
     * 文件上传
     * @param $path
     * @return bool|string
     * @throws \yii\base\InvalidConfigException
     */
    public static function uploadFileByPath($path){
        $file_name = uniqid().substr($path,strpos($path,"."));
        /**
         * @var QiniuOss $oss
         */
        $oss = \Yii::$app->get("oss");
        if($oss->uploadFile($file_name,$path)){
            return $oss->bucket_domain.$file_name;
        }else{
            return false;
        }
    }
}